<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Using script classes</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_use_script_class.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Using script classes </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>When there are multiple objects controlled by the same script implementation it may be favourable to use script classes, rather than global script functions. Using script classes each instance can have it's own set of variables within the class, contrary to the global functions that needs to rely on global variables to store persistent information.</p>
<p>Of course, it would be possible to duplicate the script modules, so that there is one module for each object instance, but that would be impose a rather big overhead for the application. Script classes don't have that overhead, as all instances share the same module, and thus the same bytecode and function ids, etc.</p>
<h2><a class="anchor" id="doc_use_script_class_1"></a>
Instanciating the script class</h2>
<p>Before instanciating the script class you need to know which class to instanciate. Exactly how this is done depends on the application, but here are some suggestions.</p>
<p>If the application knows the name of the class, either hardcoded or from some configuration, the class type can easily be obtained by calling the module's <a class="el" href="classas_i_script_module.html#a7fbc2bd888b248d2c2ee2d953b49eefc">GetTypeIdByDecl</a> with the name of the class. The application can also choose to identify the class through some properties of the class, e.g. if the class implements a predefined <a class="el" href="classas_i_script_engine.html#ae2d89b82561b7f9843f35693c664589f">interface</a>. Then the application can enumerate the class types implemented in the script with <a class="el" href="classas_i_script_module.html#a33ebba99a6358ccfb0c154fca7f58f77">GetObjectTypeByIndex</a> and then examine the type through the <a class="el" href="classas_i_object_type.html">asIObjectType</a> interface.</p>
<p>A third option, if you're using the <a class="el" href="doc_addon_build.html">script builder add-on</a>, is to use the metadata to identify the class. If you choose this option, use the <a class="el" href="classas_i_script_module.html">asIScriptModule</a> to enumerate the declared types and then query the <a class="el" href="doc_addon_build.html">CScriptBuilder</a> for their metadata.</p>
<p>Once the object type is known you create the instance by calling the class' factory function, passing it the necessary arguments, e.g. a pointer to the application object which the script class should be bound to. The factory function id is found by querying the <a class="el" href="classas_i_object_type.html">asIObjectType</a>.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Get the object type</span>
<a class="code" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> *module = engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4" title="Return an interface pointer to the module.">GetModule</a>(<span class="stringliteral">&quot;MyModule&quot;</span>);
<a class="code" href="classas_i_object_type.html" title="The interface for an object type.">asIObjectType</a> *type = engine-&gt;<a class="code" href="classas_i_script_engine.html#a52f3e08240c9fc493793036721cbd5ce" title="Returns the object type interface for type.">GetObjectTypeById</a>(module-&gt;<a class="code" href="classas_i_script_module.html#a7fbc2bd888b248d2c2ee2d953b49eefc" title="Returns a type id by declaration.">GetTypeIdByDecl</a>(<span class="stringliteral">&quot;MyClass&quot;</span>));

<span class="comment">// Get the factory function from the object type</span>
<a class="code" href="classas_i_script_function.html" title="The interface for a script function description.">asIScriptFunction</a> *factory = type-&gt;<a class="code" href="classas_i_object_type.html#a53c799db82ab17dd3f3ee8471b53370b" title="Returns the factory function by the declaration.">GetFactoryByDecl</a>(<span class="stringliteral">&quot;MyClass @MyClass()&quot;</span>);

<span class="comment">// Prepare the context to call the factory function</span>
ctx-&gt;<a class="code" href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a" title="Prepares the context for execution of the function.">Prepare</a>(factory);

<span class="comment">// Execute the call</span>
ctx-&gt;<a class="code" href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84" title="Executes the prepared function.">Execute</a>();

<span class="comment">// Get the object that was created</span>
<a class="code" href="classas_i_script_object.html" title="The interface for an instance of a script object.">asIScriptObject</a> *obj = *(<a class="code" href="classas_i_script_object.html" title="The interface for an instance of a script object.">asIScriptObject</a>**)ctx-&gt;<a class="code" href="classas_i_script_context.html#a889bf11123beba669637849c8e9b2b86" title="Returns the address of the returned value.">GetAddressOfReturnValue</a>();

<span class="comment">// If you&#39;re going to store the object you must increase the reference,</span>
<span class="comment">// otherwise it will be destroyed when the context is reused or destroyed.</span>
obj-&gt;<a class="code" href="classas_i_script_object.html#a3e08890e31163e4d33c0f27dc9072662" title="Increase reference counter.">AddRef</a>();
</pre></div><p>The factory function is <a class="el" href="doc_call_script_func.html">called as a regular global function</a> and returns a handle to the newly instanciated class.</p>
<h2><a class="anchor" id="doc_use_script_class_2"></a>
Calling a method on the script class</h2>
<p>Calling the methods of the script classes are similar to <a class="el" href="doc_call_script_func.html">calling global functions</a> except that you obtain the function id from the <a class="el" href="classas_i_object_type.html">asIObjectType</a>, and you must set the object pointer along with the rest of the function arguments.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Obtain the function object that represents the class method</span>
<a class="code" href="classas_i_script_function.html" title="The interface for a script function description.">asIScriptFunction</a> *func = type-&gt;<a class="code" href="classas_i_object_type.html#a3b3bdbd8c4290724cc396cede83fa089" title="Returns the method by declaration.">GetMethodByDecl</a>(<span class="stringliteral">&quot;void method()&quot;</span>);

<span class="comment">// Prepare the context for calling the method</span>
ctx-&gt;<a class="code" href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a" title="Prepares the context for execution of the function.">Prepare</a>(func);

<span class="comment">// Set the object pointer</span>
ctx-&gt;<a class="code" href="classas_i_script_context.html#a10d3c152b25d07584999f4d9fe5ce8b1" title="Sets the object for a class method call.">SetObject</a>(obj);

<span class="comment">// Execute the call</span>
ctx-&gt;<a class="code" href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84" title="Executes the prepared function.">Execute</a>();
</pre></div><h2><a class="anchor" id="doc_use_script_class_3"></a>
Receiving script classes</h2>
<p>In order for the application to register a function that receives a script class it must first know the type. Of course, since the class is declared in the script it isn't possible to know the type before the script is compiled. Instead the application can register an interface with the engine. The function can then be registered to receive a handle to that interface.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Register an interface</span>
engine-&gt;<a class="code" href="classas_i_script_engine.html#ae2d89b82561b7f9843f35693c664589f" title="Registers an interface.">RegisterInterface</a>(<span class="stringliteral">&quot;IMyObj&quot;</span>);

<span class="comment">// You can also register methods with the interface if you wish to force the script class to implement them</span>
engine-&gt;<a class="code" href="classas_i_script_engine.html#a43bd2c12c94a55c22be76d209de93f1a" title="Registers an interface method.">RegisterInterfaceMethod</a>(<span class="stringliteral">&quot;IMyObj&quot;</span>, <span class="stringliteral">&quot;void RequiredMethod()&quot;</span>);

<span class="comment">// Register a function that receives a handle to the interface</span>
engine-&gt;<a class="code" href="classas_i_script_engine.html#a754fafd069d8e0c19baff2dc222893b0" title="Registers a global function.">RegisterGlobalFunction</a>(<span class="stringliteral">&quot;void ReceiveMyObj(IMyObj @obj)&quot;</span>, <a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae" title="Returns an asSFuncPtr representing the function specified by the name.">asFUNCTION</a>(ReceiveMyObj), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4" title="A cdecl function.">asCALL_CDECL</a>);
</pre></div><p>The function that receives the interface should be implemented to take a pointer to an <a class="el" href="classas_i_script_object.html">asIScriptObject</a>.</p>
<div class="fragment"><pre class="fragment"><a class="code" href="classas_i_script_object.html" title="The interface for an instance of a script object.">asIScriptObject</a> *gObj = 0;
<span class="keywordtype">void</span> ReceiveMyObj(<a class="code" href="classas_i_script_object.html" title="The interface for an instance of a script object.">asIScriptObject</a> *obj)
{
  <span class="comment">// Do something with the object</span>
  <span class="keywordflow">if</span>( obj )
  {
    <span class="keywordflow">if</span>( doStore )
    {
      <span class="comment">// If the object is stored, we shouldn&#39;t release the handle</span>
      gObj = obj;
    }
    <span class="keywordflow">else</span>
    {
      <span class="comment">// If the object is not stored, we must release the handle before returning</span>
      obj-&gt;<a class="code" href="classas_i_script_object.html#a4bed3c3ac9f16294985835747aa122d3" title="Decrease reference counter.">Release</a>();
    }
  }
}
</pre></div><p>If you don't want to use interfaces like this, then you may want to look into the <a class="el" href="doc_adv_var_type.html">variable argument type</a> or the generic container <a class="el" href="doc_addon_any.html">any add-on</a>, which are ways that can be used to receive values and objects of which the type is not known beforehand. </p>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
